//
//  CustomProductViewController.m
//  iShop
//
//  Created by svp on 17/12/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "CustomProductViewController.h"
#import "ProductsViewController.h"
#import "HobbiesViewController.h"
#import "SearchViewController.h"
#import "iShopAppDelegate.h"
#import "CoreDataUtils.h"
#import "Utils.h"
#import "UIImageEx.h"
#import "ConstansDefine.h"

@implementation CustomProductViewController

@synthesize scrollView = _scrollView;
@synthesize shop = _shop;
@synthesize rootCategory = _rootCategory;
@synthesize hobbiesButton = _hobbiesButton;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

-(void) prepareData
{
	if (!_categories) {
		_categories = [[NSMutableArray alloc] init];
	}
	[_categories removeAllObjects];
	NSLog(@"count:%i",[CoreDataUtils getSubCategoryFromShop:_shop rootCategory:_rootCategory].count);
	[_categories addObjectsFromArray:[CoreDataUtils getSubCategoryFromShop:_shop rootCategory:_rootCategory]];
	_numCustomProducts = _categories.count;
	NSLog(@"_numCustomProducts:%i",_numCustomProducts);
	int row = _numCustomProducts/2;
	if(_numCustomProducts%2 != 0)
	{
		row++;
	}
	CGFloat imageWidth = 150;
	CGFloat imageHeight = (372-15) / 2;
 	_scrollView.contentSize = CGSizeMake(320, row*imageHeight + (row + 1) * 5);
	for (UIView *v in _scrollView.subviews) {
		[v removeFromSuperview];
	}
	//NSArray *arr = [ NSArray arrayWithObjects:@"subcat1.jpg", @"subcat2.jpg", @"subcat3.jpg", @"subcat4.jpg", @"subcat5.jpg", @"subcat6.jpg", @"subcat7.jpg", @"subcat8.jpg", @"subcat9.jpg", @"subcat10.jpg", @"subcat11.jpg", nil];
	for (int index = 0; index < _numCustomProducts; index++) {
		Category *category = [_categories objectAtIndex:index];
		CustomeUIImageView *temp = [[CustomeUIImageView alloc] initWithImage:[[Utils getImageOf:@"category" withName:category.CategoryImage] scaleToSize:CGSizeMake(imageWidth, imageHeight)]];
		//CustomeUIImageView *temp = [[CustomeUIImageView alloc] initWithImage:[[Utils getImageOf:@"category" withName:[arr objectAtIndex:index]] scaleToSize:CGSizeMake(100, 135)]];

		temp.delegate = self;
		temp.index = index;
		[_scrollView addSubview:temp];
		temp.frame = CGRectMake(5*((index%2)+1) + imageWidth*(index%2), 5*((index/2)+1) + imageHeight*(index/2), imageWidth, imageHeight);
		temp.userInteractionEnabled = YES;
	}
	
}

-(void) selectCustomeUIImageView:(CustomeUIImageView*)indexView
{
	NSLog(@"I'm %i",indexView.index);
	if (_selectedUIImageView) {
		[_selectedUIImageView.layer setBorderColor: [[UIColor clearColor] CGColor]];
		[_selectedUIImageView.layer setBorderWidth: 8.0];		
	}
	_selectedUIImageView = indexView;
	[_selectedUIImageView.layer setBorderColor: [[UIColor blueColor] CGColor]];
	[_selectedUIImageView.layer setBorderWidth: 8.0];
	
	ProductsViewController *custom = [[ProductsViewController alloc] initWithNibName:@"ProductsViewController" bundle: nil];
	custom.shop = _shop;
	custom.subCategory = [_categories objectAtIndex:indexView.index];
	[self.navigationController pushViewController:custom animated:YES];
	[custom release];
}

-(void) viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];
	backButton.title = @"Back";
	self.navigationItem.backBarButtonItem = backButton;
	[backButton release];
	
	NSObject *ksession = [Utils getValueForRefKey:kSessionKey];
	if (!ksession || ksession == [NSNull null]) {
		_hobbiesButton.enabled = NO;
	}
	else {
		_hobbiesButton.enabled= YES;
	}
}

-(void) viewDidLoad
{
	[super viewDidLoad];
	self.title = @"Category";
	_selectedUIImageView = nil;
	_numCustomProducts = 11;
	[self prepareData];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	NSLog(@"CustomProductViewController  dealloc");
	for (UIView *v in _scrollView.subviews){
		[v removeFromSuperview];
		[v release];
	}
	[_scrollView release];
    [super dealloc];
}

-(IBAction) openHobbies:(id) sender
{
	NSLog(@"openHobbies");
	HobbiesViewController *custom = [[HobbiesViewController alloc] initWithNibName:@"HobbiesViewController" bundle: nil];
	UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:custom];
	[self.navigationController presentModalViewController:nav animated:YES];
	[custom release];
	[nav release];
}

-(IBAction) openSearchScreen:(id) sender
{
	NSLog(@"openSearchScreen");
	SearchViewController *custom = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle: nil];
	UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:custom];
	[self.navigationController presentModalViewController:nav animated:NO];
	//[self.navigationController pushViewController:custom animated:YES];
	[custom release];
	[nav release];
}

-(IBAction) homePress:(id) sender
{
	[self.navigationController popToRootViewControllerAnimated:YES];
}

@end
